package com.chukun.flink.stream.connector.sink;

import com.chukun.flink.stream.bean.KafkaMess;
import com.google.gson.Gson;
import org.apache.flink.api.common.serialization.SerializationSchema;

import java.nio.charset.StandardCharsets;

/**
 * @author chukun
 * @version 1.0.0
 * @description kafka消息序列化工具
 * @createTime 2022年05月28日 18:45:00
 */
public class KafkaSerializationSchema implements SerializationSchema<KafkaMess> {

    @Override
    public byte[] serialize(KafkaMess kafkaMess) {
        if (kafkaMess == null) {
            return new byte[0];
        }
        Gson gson = new Gson();
        String json = gson.toJson(kafkaMess);
        return json.getBytes(StandardCharsets.UTF_8);
    }
}
